/** INCLUDES *******************************************************/
#include "GenericTypeDefs.h"
#include "Compiler.h"
#include "DAC.h"
#include "HardwareProfile.h"

#include <i2c.h>

/** D E C L A R A T I O N S **************************************************/
#pragma code

void InitDAC(void)
{
	//here is the I2C setup from the Seeval32 code.
	TRISBbits.TRISB0 = 1; //Configure SCL
							//as Input
	TRISBbits.TRISB1 = 1; //Configure SDA
							//as Input

	SSPSTAT = 0x80;   //Disable SMBus &
					//Slew Rate Control
	SSPCON1 = 0x28;   //Enable MSSP Master
	SSPADD = 7;    	// 11 = 1MHz, 6 = 1.7 MHz
					//Should be 0x18
					//for 100kHz
	SSPCON2 = 0x00;   //Clear MSSP Conrol Bits

	mInitDA();
}

unsigned char I2CWrite2(unsigned char addr, unsigned char data1, unsigned char data2 )
{
  IdleI2C();                          // ensure module is idle
  StartI2C();                         // initiate START condition
  while ( SSPCON2bits.SEN );          // wait until start condition is over 
  WriteI2C( addr );                 // write address byte
  IdleI2C();                          // ensure module is idle
  WriteI2C ( data1 );                  // Write data byte 
  IdleI2C();                          // ensure module is idle
  WriteI2C ( data2 );                  // Write data byte 
  IdleI2C();                          // ensure module is idle
  StopI2C();                          // send STOP condition
  while ( SSPCON2bits.PEN );          // wait until stop condition is over 
  return ( 0 );                       // return with no error
}

void SetDAC(BYTE dacno, WORD_VAL freq)
{
	mSelectDA(dacno);
	I2CWrite2(0xC2, (freq.byte.HB & 0x0F), freq.byte.LB);	// 0xC2 = 11000010, address.
}


